package com.hongshu.web.controller.sys;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.hongshu.common.core.enums.Result;
import com.hongshu.common.core.validator.myVaildator.noLogin.NoLoginIntercept;
import com.hongshu.common.core.web.controller.BaseController;
import com.hongshu.web.domain.dto.*;
import com.hongshu.web.service.sys.ISysSystemConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 系统配置
 *
 * @author: hongshu
 */
@Tag(name = "系统配置相关接口", description = "系统配置相关接口")
@RestController
@RequestMapping("/system/config")
@Slf4j
public class SysSystemConfigController extends BaseController {

    @Autowired
    private ISysSystemConfigService systemConfigService;


    @Operation(summary = "获取所有配置", description = "获取所有配置")
    @GetMapping("/allConfig")
    public Result<?> getAllConfig() {
        return Result.ok(systemConfigService.getAllConfigData());
    }

    @NoLoginIntercept
    @Operation(summary = "获取网站配置", description = "获取网站配置")
    @GetMapping("/websiteConfig")
    public Result<?> getWebsiteConfig() {
        return Result.ok(systemConfigService.getWebsiteConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取系统配置", description = "获取系统配置")
    @GetMapping("/systemConfig")
    public Result<?> getSystemConfig() {
        return Result.ok(systemConfigService.getSystemConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取本地存储配置", description = "获取本地存储配置")
    @GetMapping("/localConfig")
    public Result<?> getLocalConfig() {
        return Result.ok(systemConfigService.getLocalConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取oss配置", description = "获取oss配置")
    @GetMapping("/ossConfig")
    public Result<?> getOssConfig() {
        return Result.ok(systemConfigService.getOssConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取短信配置", description = "获取短信配置")
    @GetMapping("/smsConfig")
    public Result<?> getSmsConfig() {
        return Result.ok(systemConfigService.getSmsConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取支付宝配置", description = "获取支付宝配置")
    @GetMapping("/alipayConfig")
    public Result<?> getAlipayConfig() {
        return Result.ok(systemConfigService.getAlipayConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取验证码配置", description = "获取验证码配置")
    @GetMapping("/captchaConfig")
    public Result<?> getCaptchaConfig() {
        return Result.ok(systemConfigService.getCaptchaConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取高德地图配置", description = "获取高德地图配置")
    @GetMapping("/amapConfig")
    public Result<?> getAmapConfig() {
        return Result.ok(systemConfigService.getAmapConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取演示账号配置", description = "获取演示账号配置")
    @GetMapping("/demoAccountConfig")
    public Result<?> getDemoAccountConfig() {
        return Result.ok(systemConfigService.getDemoAccountConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取演示站配置", description = "获取演示站配置")
    @GetMapping("/demoSiteConfig")
    public Result<?> getDemoSiteConfig() {
        return Result.ok(systemConfigService.getDemoSiteConfig());
    }

    @NoLoginIntercept
    @Operation(summary = "获取黑名单列表", description = "获取黑名单列表")
    @GetMapping("/blacklist")
    public Result<?> getBlacklistConfig() {
        return Result.ok(systemConfigService.getBlacklistConfig());
    }


    @Operation(summary = "更新网站配置", description = "更新网站配置")
    @PostMapping("/website")
    public Result<?> updateWebsiteConfig(@RequestBody WebsiteConfigDTO websiteConfig) {
        systemConfigService.updateWebsiteConfig(websiteConfig);
        return Result.ok();
    }

    @Operation(summary = "更新系统配置", description = "更新系统配置")
    @PostMapping("/system")
    public Result<?> updateSystemConfig(@RequestBody SystemConfigDTO systemConfig) {
        systemConfigService.updateSystemConfig(systemConfig);
        return Result.ok();
    }

    @Operation(summary = "更新本地存储配置", description = "更新本地存储配置")
    @PostMapping("/local")
    public Result<?> updateLocalConfig(@RequestBody LocalConfigDTO localConfig) {
        systemConfigService.updateLocalConfig(localConfig);
        return Result.ok();
    }

    @Operation(summary = "更新oss配置", description = "更新oss配置")
    @PostMapping("/oss")
    public Result<?> updateOssConfig(@RequestBody Map<String, Object> request) {
        String provider = (String) request.get("provider");
        OssConfigDTO ossConfig = new ObjectMapper().convertValue(request.get("config"), OssConfigDTO.class);
        systemConfigService.updateOssConfig(provider, ossConfig);
        return Result.ok();
    }

    @Operation(summary = "更新短信配置", description = "更新短信配置")
    @PostMapping("/sms")
    public Result<?> updateSmsConfig(@RequestBody Map<String, Object> request) {
        String provider = (String) request.get("provider");
        SmsConfigDTO smsConfig = new ObjectMapper().convertValue(request.get("config"), SmsConfigDTO.class);
        systemConfigService.updateSmsConfig(provider, smsConfig);
        return Result.ok();
    }

    @Operation(summary = "更新支付宝配置", description = "更新支付宝配置")
    @PostMapping("/alipay")
    public Result<?> updateAlipayConfig(@RequestBody AlipayConfigDTO alipayConfig) {
        systemConfigService.updateAlipayConfig(alipayConfig);
        return Result.ok();
    }

    @Operation(summary = "更新验证码配置", description = "更新验证码配置")
    @PostMapping("/captcha")
    public Result<?> updateCaptchaConfig(@RequestBody CaptchaConfigDTO captchaConfig) {
        systemConfigService.updateCaptchaConfig(captchaConfig);
        return Result.ok();
    }

    @Operation(summary = "更新高德地图配置", description = "更新高德地图配置")
    @PostMapping("/amap")
    public Result<?> updateAmapConfig(@RequestBody AmapConfigDTO amapConfig) {
        systemConfigService.updateAmapConfig(amapConfig);
        return Result.ok();
    }

    @Operation(summary = "更新演示账号配置", description = "更新演示账号配置")
    @PostMapping("/demoAccount")
    public Result<?> updateDemoAccount(@RequestBody DemoAccountConfigDTO demoAccountConfig) {
        systemConfigService.updateDemoAccount(demoAccountConfig);
        return Result.ok();
    }

    @Operation(summary = "更新演示站配置", description = "更新演示站配置")
    @PostMapping("/demoSite")
    public Result<?> updateDemoSite(@RequestBody DemoSiteConfigDTO demoSiteConfig) {
        systemConfigService.updateDemoSite(demoSiteConfig);
        return Result.ok();
    }

    @Operation(summary = "更新黑名单启用状态", description = "更新黑名单功能启用状态")
    @PostMapping("/blacklist/enabled")
    public Result<?> updateBlacklistEnabled(@RequestBody Map<String, Boolean> request) {
        Boolean enabled = request.get("enabled");
        systemConfigService.updateBlacklistEnabled(enabled);
        return Result.ok();
    }

    @Operation(summary = "添加黑名单项", description = "添加IP黑名单")
    @PostMapping("/blacklist/add")
    public Result<?> addBlacklistItem(@RequestBody @Valid BlacklistItemDTO blacklistItemDTO) {
        systemConfigService.addBlacklistItem(blacklistItemDTO);
        return Result.ok();
    }

    @Operation(summary = "解封黑名单项", description = "解封指定IP")
    @PostMapping("/blacklist/{id}/unban")
    public Result<?> unbanBlacklistItem(@PathVariable Long id) {
        systemConfigService.unbanBlacklistItem(id);
        return Result.ok();
    }

    @Operation(summary = "删除黑名单项", description = "删除指定黑名单项")
    @DeleteMapping("/blacklist/{id}")
    public Result<?> deleteBlacklistItem(@PathVariable Long id) {
        systemConfigService.deleteBlacklistItem(id);
        return Result.ok();
    }
}

